package com.hjq.controller;

import com.hjq.common.Result;
import com.hjq.entity.vo.UserInfoVO;
import com.hjq.entity.vo.UserLoginVO;
import com.hjq.entity.vo.UserRegisterVO;
import com.hjq.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 登录与注册
 */
@Slf4j
@RestController
public class LoginController {
    @Autowired
    private UserService userService;
    /**
     * 登录
     * @param user 登录用户信息
     * @return 用户信息
     */
    @PostMapping("/login")
    public Result<UserInfoVO> login(UserLoginVO user){
        log.info("用户登录,{}",user);
        UserInfoVO login = userService.login(user);
        if(login == null){
            return Result.error("用户名或密码错误");
        }
        return Result.success(login);
    }
    /**
     * 注册
     * @param user 注册用户信息
     * @return 注册结果
     */
    @PostMapping("/register")
    public Result<Object> register(@RequestBody UserRegisterVO user){
        log.info("用户注册,{}",user);
        userService.register(user);
        return Result.success("注册成功");
    }
}
